<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>登录</title>
  <style>
    .login-container {
      width: 400px;
      height: 400px;
      margin: 100px auto;
    }
  </style>
</head>

<body>

  <div class="login-container">
    <button class="getLoginUrlBtn">获取登录二维码</button>
  </div>


  <script src="./js/socketIo.js"></script>
  <script src="./js/qrcode.js"></script>
  <script src="./js/md5.js"></script>
  <script>
    // 判断是否登录
    (async () => {
      const { data } = await fetch("/api/weChaty/status").then((res) => res.json());
      if (data) {
        location.href = "/"
        return;
      }

      const btn = document.querySelector(".getLoginUrlBtn");
      const wrapper = document.querySelector(".login-container")
      const pass = md5(prompt("请输入密码:"))
      if (pass === "ce1c1cdc2fac8e1167f22cd4bd88d324") {

        let socket = io();

        socket.on("login", (url) => {
          // 拿到登录二维码
          new QRCode(wrapper, url)
          btn.remove()
        })

        socket.on("loginSuccess", () => {
          // 登录成功
          wrapper.innerHTML = "登录成功";
          alert("登录成功");
          location.href = "/"
        })

        btn.addEventListener("click", () => {
          socket.emit("login")
        })
      } else {
        wrapper.innerText = "密码错误";
      }
    })()


  </script>
</body>

</html>